#! /usr/bin/env python
# coding=utf-8
# 公用方法

def getVersion():
	return '1.1.2'

def getAppName():
	return 'Beikx Cron'

def getCopyRight():
	return "copyright &copy; beikx.com 2010 Version %s" % getVersion()

def toLongList(strList):
	ls = []
	if strList:
		for v in strList:
			lv = long(v)
			if lv >= 0:
				ls.append(long(v))
		if len(ls) == 0:
			ls.append(-1)
	return ls

def longListToStr(longList):
	s = ''
	if longList and len(longList) > 0:
		for v in longList:
			s += ',' + str(v)
		s = s[1:]
	return s

def encodeJsString(s):
	if s:
		return s.replace("\\", "\\\\").replace("\"", "\\\"").replace("'", "\\'").replace("\r\n", "\\n").replace("\n", "\\n").replace("\r", "")
	else:
		return ""

def writeHtml(self, head, body, startupJs):
	self.response.headers['Content-Type'] = 'text/html'
	self.response.out.write("""<html>
<head>
	<title>%s</title>
	<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
	%s
</head>
<body>
	%s
	%s
</body>
</html>""" % (getAppName(), head, body, startupJs))

def writeNoPermissionMessage(self):
	self.response.headers['Content-Type'] = 'text/html'
	self.response.out.write("""<html>
<head>
	<title>%s</title>
	<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>
<body>
	<div>
	You are no permission.
	</div>
</body>
</html>""" % getAppName())

htmlCodes = (
	('&', '&amp;'),
	('<', '&lt;'),
	('>', '&gt;'),
	('"', '&quot;'),
	("'", '&#39;')
)
htmlCodesReversed  = (
	('&amp;', '&'),
	('&lt;', '<'),
	('&gt;', '>'),
	('&quot;', '"'),
	('&#39;', "'")
)

def htmlDecode(s, codes=htmlCodesReversed):
	""" Returns the ASCII decoded version of the given HTML string. This does 
	NOT remove normal HTML tags like <p>. It is the inverse of htmlEncode()."""
	for code in codes:
		s = s.replace(code[1], code[0])
	return s

def htmlEncode(s, codes=htmlCodes):
	""" Returns the HTML encoded version of the given string. This is useful to
		display a plain ASCII text string on a web page."""
	for code in codes:
		s = s.replace(code[0], code[1])
	return s
